#!/bin/bash

source /etc/profile

GO_VERSION="go1.21.3"
GO_PATH="$HOME/go"

function install_go() {
    if type go >/dev/null 2>&1; then
        local cur_go_ver=$(go version | awk '{print $3}')
        if [ $GO_VERSION == $cur_go_ver ]; then
            echo "$GO_VERSION has been installed"
            return 0
        else
            echo "Remove cur golang"
            sudo rm -rf /usr/local/go
        fi
    fi

    echo "Installing $GO_VERSION ..."
    # wget https://studygolang.com/dl/golang/${GO_VERSION}.linux-amd64.tar.gz --no-check-certificate && \
    wget https://mirrors.aliyun.com/golang/${GO_VERSION}.linux-amd64.tar.gz > /dev/null 2>&1 && \
    sudo tar -xzf ${GO_VERSION}.linux-amd64.tar.gz -C /usr/local/
	rm ${GO_VERSION}.linux-amd64.tar.gz

	# add env PATH
	[ -z "$(grep 'export GOROOT=/usr/local/go' /etc/profile)" ] && echo "export GOROOT=/usr/local/go" | sudo tee -a /etc/profile
	[ -z "$(grep 'export GOPATH=' /etc/profile)" ] && echo "export GOPATH=$GO_PATH" | sudo tee -a /etc/profile
	[ -z "$(grep 'export PATH=/usr/local/go/bin:' /etc/profile)" ] && echo 'export PATH=/usr/local/go/bin:$PATH' | sudo tee -a /etc/profile
	source /etc/profile

    go env -w GOPROXY=https://goproxy.io,direct
    go env -w GO111MODULE=on
    [ ! -d "$GO_PATH/src" ] && mkdir -p $GO_PATH/src
}

function install_docker() {
    sudo apt-get -y install apt-transport-https ca-certificates curl software-properties-common
    if [ $? -ne 0 ]; then
        echo "install docker-ce's dep error"
        exit -1
    fi 

    if ! type docker >/dev/null 2>&1; then
        curl -fsSL http://mirrors.aliyun.com/docker-ce/linux/ubuntu/gpg | sudo apt-key add -
        echo '\n' | sudo add-apt-repository "deb [arch=amd64] http://mirrors.aliyun.com/docker-ce/linux/ubuntu $(lsb_release -cs) stable"
        sudo apt-get update
        sudo apt-get -y install docker-ce
        if [ $? -ne 0 ]; then
            echo "install docker-ce error"
            exit -1
        fi
        echo 'docker installed'
    else
        echo 'docker has been installed'
    fi

    # add current user to user group
    sudo groupadd docker
    sudo usermod -aG docker $USER

    # config Docker origin
    if [ ! -e "/etc/docker/daemon.json" ]; then
        # 1. change docker's mirror to ustc's mirror
        if [ ! -d "/etc/docker" ]; then
            sudo mkdir /etc/docker
        fi
        echo "{
    \"registry-mirrors\": [\"https://docker.mirrors.ustc.edu.cn\"]
}" | sudo tee -a /etc/docker/daemon.json
        # 2. restart service
        sudo systemctl daemon-reload
        sudo systemctl restart docker
    fi

    # install Docker-compose
    # 1. install pip & change pip source
    sudo apt-get -y install python3-pip
    if [ $? -ne 0 ]; then
        echo "install python3-pip error"
        exit -1
    fi

    if [ ! -d "$HOME/.pip" ]; then
        mkdir $HOME/.pip
    fi
    if [ ! -e "$HOME/.pip/pip.conf" ]; then
    echo "[global]
index-url = https://mirrors.aliyun.com/pypi/simple/
[install]
trusted-host=mirrors.aliyun.com" > $HOME/.pip/pip.conf
    fi

    # 2. install docker-compose
    sudo apt-get -y install docker-compose
    if [ $? -ne 0 ]; then
        echo "docker-compose install error"
        exit -1
    fi
    # 3. watch docker-compose version
    docker-compose -version
}

function install_other_deps() {
    sudo apt-get -y install jq
}

# set -x

# install go & docker & other deps
install_go
# install_docker
# install_other_deps